package libofei.common.utils;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;

import java.io.File;

/**
 * @ClassName TencentBucketUtil  腾讯云存储桶工具类
 * @Description
 * @Author LIBOFEI
 * @Date 2020/5/27 16:06
 * @Version 1.0
 **/
public class TencentBucketUtil {

    /** *
     * @Description 上传图片方法
     * @Author LIBOFEI
     * @Date 16:08 2020/5/27
     * @return void
     * @Version  1.0
    **/
    public static void uploadPicture(){

        // 1 初始化用户身份信息（secretId, secretKey）。
        String secretId = "";
        String secretKey = "";
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region("ap-beijing");
        ClientConfig clientConfig = new ClientConfig(region);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);

        // 指定要上传的文件
        File localFile = new File("/Users/mac/Desktop/图片/个人图片/logo2.jpeg");
        // 指定要上传到的存储桶
        String bucketName = "";
        // 指定要上传到 COS 上对象键
        String key = "/common/logo2.jpeg";
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        System.out.println(putObjectResult);
    }


    public static void main(String[] args) {
        uploadPicture();
    }


}
